package cn.com.codingce.student.servlet;

import cn.com.codingce.student.entity.Login;
import cn.com.codingce.student.service.ITeacherService;
import cn.com.codingce.student.service.impl.TeacherServiceImpl;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        int id = Integer.parseInt(request.getParameter("id"));
        String uname = request.getParameter("uname");
        String upwd = request.getParameter("upwd");


        Login tea = new Login(id, uname, upwd);
        ITeacherService teaSer = new TeacherServiceImpl();

        boolean result = teaSer.addTeacher(tea);
        /*
         * out  request  response session application
         * out: PrintWriter out = response.getWriter();
         * session: request.getSession()
         * application: request.getServletContext();
         *
         */
        response.setContentType("text/html; charset=UTF-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();


        if (result) {
            //out.println("增加成功");
            response.sendRedirect("login.jsp");
        } else {
            //out.println("添加失败");
            response.sendRedirect("register.jsp");
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
